package com.aube.plugin.rank.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.huyn.baseframework.dynamicload.DLView;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by huyaonan on 16/5/13.
 */
public class RankProgress extends DLView {
    private Paint mPaint;
    private int mDefaultColor=0xffff0000;
    private float mProgressRatio=0f;
    private int mProgressRadius=4;
    private float mCurrentProgress=0;
    private AtomicBoolean mAnimating = new AtomicBoolean(false);
    public RankProgress(Context context) {
        this(context, null);
    }
    public RankProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public RankProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        mPaint = new Paint();
        mPaint.setColor(mDefaultColor);
        mPaint.setAntiAlias(true);
    }

    public void updateColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        RectF rect = new RectF(0, 0, width*mProgressRatio, height);

        canvas.drawRoundRect(rect, mProgressRadius, mProgressRadius, mPaint);
    }

    public void animTo(final float targetProgress) {
        if(targetProgress == mCurrentProgress)
            return;
        if(mAnimating.get())
            return;
        mAnimating.set(true);
        final ValueAnimator anim = ValueAnimator.ofFloat(mCurrentProgress, targetProgress);
        anim.setDuration(400);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mProgressRatio = value;
                invalidate();
            }
        });
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mCurrentProgress = targetProgress;
                mAnimating.set(false);
            }
        });
        anim.start();
    }

    public void anim(float from, final float target) {
        if(mAnimating.get())
            return;
        mAnimating.set(true);
        final ValueAnimator anim = ValueAnimator.ofFloat(from, target);
        anim.setDuration(400);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mProgressRatio = value;
                invalidate();
            }
        });
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mCurrentProgress = target;
                mAnimating.set(false);
            }
        });
        anim.start();
    }

}
